import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import { message } from 'antd';
import { useRef, useMemo } from 'react';
import { useRequest } from 'umi';
import { Search } from '../effects';
import { SchemaManageAPI } from '../type';

export const useSearchProps = (props: SchemaManageAPI.ISearchPropsType): any => {
  const ref = useRef<Search>(new Search());

  const searchProps = useMemo(() => {
    const newSearchList: SchemaManageAPI.ISearchList[] = props?.searchList?.filter(
      (item: any) => item?.componentType != 'QueryParams',
    );
    ref.current.mount({
      ...props,
      searchList: newSearchList,
    });
    return ref.current;
  }, [props]);

  return {
    searchProps: searchProps,
  };
};

/**
 *  获取组织机构岗位接口
 */
const GetFlowOrgTreeByOrgID = () => {
  const flowOrgTreeService = useRequest(
    (payload: Record<string, any>) => {
      let reqUrl = `${getBaseUrl()}/commonController/getFlowOrgTreeByOrgID/${payload?.params}/${payload?.flowType}/${
        payload?.orgID
      }`;
      let methodType = 'GET';
      return requestFunComponFiex(methodType, reqUrl, {});
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (!res?.msgFlag) {
          message.error(res?.msg);
        }
      },
    },
  );

  // 处理数据
  let flowOrgTreeList = useMemo(() => {
    if (flowOrgTreeService?.data?.msgFlag) {
      return flowOrgTreeService?.data?.obj;
    } else {
      return [];
    }
  }, [flowOrgTreeService?.data]);

  return {
    flowOrgTreeRun: flowOrgTreeService?.run,
    flowOrgTreeLoading: flowOrgTreeService?.loading,
    flowOrgTreeList: flowOrgTreeList,
  };
};

/**
 * 获取用户数据 /commonController/getUserList
 */
const getUserDataList = () => {
  const getUserServices = useRequest(
    (bodyParams: any) => {
      let reqUrl = `${getBaseUrl()}/commonController/getUserList`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (!res?.msgFlag) {
          message.error(res?.msg);
        }
      },
    },
  );
  // 处理数据
  let getUserList = useMemo(() => {
    if (getUserServices?.data?.msgFlag) {
      return getUserServices?.data?.rows;
    } else {
      return [];
    }
  }, [getUserServices?.data]);

  return {
    getUserRun: getUserServices?.run,
    getUserLoading: getUserServices?.loading,
    getUserList: getUserList,
  };
};

export { GetFlowOrgTreeByOrgID, getUserDataList };
